package Servlet; /**
 * 项目名称：addServlet.java
 * 文件名称：addServlet.java
 * 类的描述：无
 * 创建时间：2025-07-18 17:54:47
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */

import java.io.*;

import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "addServlet", value = "/api/add")
public class addServlet extends HttpServlet {
    public void init() {
    }


    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String sex = req.getParameter("sex");
        String pass = req.getParameter("pass");
        GokJDBCUtil2.update("insert into user(name,age,sex,pass) values(?,?,?,?)", name, age, sex,pass);
        //重定向
        res.sendRedirect("/app/api/testpost?page=1");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("application/json; charset=UTF-8");
        res.setContentType("text/html; charset=UTF-8");
        String method = req.getMethod();
        if (method.equals("POST")) {
            this.doPost(req, res);
        } else {
            PrintWriter writer = res.getWriter();
            res.setCharacterEncoding("utf-8");
            res.setContentType("application/json");
            writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}